package com.frank.loadinglibrary.base.baseanimations;

import ohos.agp.animation.Animator;

import java.util.ArrayList;

/**
 * @author : frank
 * @date : 2021/4/29 16:05
 */
public class BaseAnimationController<T extends Animator> implements BaseAnimation {

    private ArrayList<T> mAnimators;

    private boolean mHasAnimators;

    private IAnimationController controll;

    public BaseAnimationController(IAnimationController controll) {
        this.controll = controll;
    }

    @Override
    public void startAnimators() {
        ensureAnimators();
        if (mAnimators == null) {
            return;
        }
        if (isAnimationRunning()) {
            return;
        }
        for (int i = 0; i < mAnimators.size(); i++) {
            T animator = mAnimators.get(i);
            animator.start();
        }
    }

    @Override
    public void stopAnimators() {
        if (mAnimators != null) {
            for (T animator : mAnimators) {
                if (animator != null && animator.isRunning()) {
                    try {
                        animator.cancel();
                        animator.end();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    @Override
    public void pauseAnimation() {
        if (mAnimators != null) {
            for (T animator : mAnimators) {
                if (animator != null && !animator.isPaused()) {
                    try {
                        animator.pause();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    @Override
    public void resumeAnimation() {
        if (mAnimators != null) {
            for (T animator : mAnimators) {
                if (animator != null && animator.isPaused()) {
                    try {
                        animator.resume();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    @Override
    public void resetAnimation() {
        if (controll != null) {
            controll.onAnimationReset();
        }
    }


    private void ensureAnimators() {
        if (!mHasAnimators && controll != null) {
            mAnimators = controll.onCreateAnimators();
            if (mAnimators != null) {
                mHasAnimators = true;
            }
        }
    }

    @Override
    public boolean isAnimationRunning() {
        if (mAnimators != null) {
            for (T animator : mAnimators) {
                return animator.isRunning();
            }
        }
        return false;
    }


    public void release() {
        if (mAnimators != null) {
            for (T animator : mAnimators) {
                animator.release();
            }
        }
        mAnimators.clear();
    }

    public interface IAnimationController<T> {

        /**
         * 创建动画
         * @return
         */
        public ArrayList<T> onCreateAnimators();

        /**
         * 重置动画
         */
        public void onAnimationReset();
    }
}
